//
//  NewTimeEventVC.m
//  timelyRain
//
//  Created by Jiafei Wei on 16/8/8.
//  Copyright © 2016年 Jiafei Wei. All rights reserved.
//

#import "NewTimeEventVC.h"
#import "SetWeekTime.h"
#import "MBTUiColor.m"
#import "DbManage.h"
#import "StringTool.h"
#import "DataTool.h"

@interface NewTimeEventVC ()<UIGestureRecognizerDelegate>{
    BOOL isOpen;
    BOOL isRemindMe;
    BOOL isUpdate;
    NSString *chooseDate;
}
@property (weak, nonatomic) IBOutlet UILabel *weekTime;
@property (weak, nonatomic) IBOutlet UIButton *isRemaind;
@property (weak, nonatomic) IBOutlet UIDatePicker *datepicker;
@property (weak, nonatomic) IBOutlet UIView *executeTime;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *timeHeight;
@property (weak, nonatomic) IBOutlet UIView *doWeek;
@property (weak, nonatomic) IBOutlet UITextField *titleThingsName;
@property (weak, nonatomic) IBOutlet UITextField *mark;
- (IBAction)Complete:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *timeText;
@property (weak, nonatomic) IBOutlet UIImageView *iv_gone;
@property (weak, nonatomic) IBOutlet UILabel *showDoTime;

@property (weak, nonatomic) IBOutlet UILabel *doTime;
- (IBAction)change:(id)sender;
@end

@implementation NewTimeEventVC

- (void)viewDidLoad {
    [super viewDidLoad];
    //[self setCustomLeftButton];
    [_datepicker addTarget:self action:@selector(chooseDate:)forControlEvents:UIControlEventValueChanged];
    isRemindMe =YES;
    [self.view setBackgroundColor:[UIColor colorwithHexString:@"#F4F3F8"]];
    self.navigationController.navigationBarHidden=NO;
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@""style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backItem;
    [self excuteTimeEvent];
    [self openWeek];
    [self setDataValue];
    //一开始就添加控件
}


-(void)setDataValue{
    if (_things!=nil){
        isUpdate =YES;
        _titleThingsName.text =_things.title;
        _mark.text =_things.remark;
        NSString *do_time =_things.doTime;
        NSString *showTime =[DataTool timestampChaStringDate:[do_time longLongValue]];
        _timeText.text =showTime;
        _weekTime.text =_things.doCircle;
        _showDoTime.text=showTime;
    }else{
        isUpdate =NO;
    }
}
-(void)chooseDate:(UIDatePicker *)sender {
    NSDate *selectedDate = sender.date;
    NSString *dateString =[DataTool getStringDate:selectedDate];
    _showDoTime.text = dateString;
    _timeText.text=dateString;
    long datelong = (long)[selectedDate timeIntervalSince1970];
    NSString *timeSp = [NSString stringWithFormat:@"%ld",datelong];//字符串的时间类型
    chooseDate =timeSp;
}
-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
}

-(void)excuteTimeEvent{
    NSLog(@"createNewEvent");
    _executeTime.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc] init];
    tapGest.numberOfTapsRequired = 1;
    tapGest.delegate = self;
    [_executeTime addGestureRecognizer:tapGest];
    [tapGest addTarget:self action:@selector(tapView:)];
}
- (void)tapView:(UITapGestureRecognizer *)tapGest{
    [self isGone];
}

-(void)openWeek{
    _doWeek.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc] init];
    tapGest.numberOfTapsRequired = 1;
    tapGest.delegate = self;
    [_doWeek addGestureRecognizer:tapGest];
    [tapGest addTarget:self action:@selector(doWeek:)];
}
- (void)doWeek:(UITapGestureRecognizer *)tapGest{
    NSLog(@"doWeek");
    SetWeekTime *newEvent = [[SetWeekTime alloc]initWithNibName:@"SetWeekTime"bundle:nil];
    [self.navigationController pushViewController:newEvent animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    self.navigationController.navigationBarHidden=YES;
}
-(void)setCustomLeftButton{//设置自定义的返回按钮
    self.title =_titleName;
    UIImage* backImage = [UIImage imageNamed:@"信息按下"];
    CGRect backframe = CGRectMake(0,0,22,22);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(doClickBackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    CGRect rect =self.navigationItem.accessibilityFrame;
    leftBarButtonItem.accessibilityFrame =CGRectMake(-30, rect.origin.y, rect.size.width, rect.size.height);
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
}

-(void)doClickBackAction:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    NSUserDefaults *dd =[NSUserDefaults standardUserDefaults];
    NSString *week =[dd objectForKey:@"week"];
    _weekTime.text =week;
}
- (IBAction)Complete:(id)sender{
    //在这里执行插入的数据
    DbManage *db = [DbManage shareDbManage];
    
    RemingThings *things = [[RemingThings alloc]init];
    if (_titleThingsName.text<0) {
        
    }
    things.title=_titleThingsName.text;
    things.doCircle =_weekTime.text;
    things.isRmeind= [NSString stringWithFormat:@"%d",isRemindMe];
    things.remark=_mark.text;
    if (chooseDate==nil) {
        things.doTime = _things.doTime;
    }else{
        things.doTime = chooseDate;
    }
    if (isUpdate){//如果更新的话
        things.id=_things.id;
        things.only=_things.only;
        things.createTime=_things.createTime;
        [db update:things];
    }else{
        things.only=[StringTool getUUIDString];
        things.createTime= [DataTool getStringTimestamp:[NSDate date]];
        [db insert:things];
    }
    [self.navigationController popViewControllerAnimated:YES];//返回去
}
-(void)isGone{
    NSLog(@"tapView");
    if (isOpen) {
        self.timeHeight.constant = self.timeHeight.constant-150;
        _showDoTime.hidden=YES;
        [UIView animateWithDuration:1.0 animations:^{
            _datepicker.alpha=0;
            [self.view layoutIfNeeded];
            isOpen =false;
        } completion:^(BOOL finished) {
            _doTime.hidden=NO;
            _timeText.hidden=NO;
            _iv_gone.hidden=NO;
        }];

    }else{
        self.timeHeight.constant = self.timeHeight.constant +150;
        _datepicker.alpha=1;
        _doTime.hidden=YES;
        _timeText.hidden=YES;
        _iv_gone.hidden=YES;
        [UIView animateWithDuration:1.0 animations:^{
            [self.view layoutIfNeeded];
            isOpen =true;
        } completion:^(BOOL finished) {
            _showDoTime.hidden=NO;
        }];
    }
}
- (IBAction)change:(id)sender {
    if (isRemindMe) {
        [_isRemaind setBackgroundImage:[UIImage imageNamed:@"关按钮"]forState:UIControlStateNormal];
        isRemindMe =NO;
    }else{
        [_isRemaind setBackgroundImage:[UIImage imageNamed:@"开按钮"]forState:UIControlStateNormal];
        isRemindMe =YES;
    }
}
@end
